define(function(require, exports, module) {
    "use strict";
    var $ = require("$"),
        app = require("dist/application/app"),
        config = app.config;
    var $talbesearch = $('form.talbe-search');

    if ($talbesearch.length > 0) {
        var $pageSize = $("input[type='radio'][name='pageSize']"),
            $pageNumber = $('#pageNumber'),
            $orderBy = $('#orderBy'),
            $order = $('#order');

        $pageSize.bind('change', function(e, p) {
            $talbesearch.submit();
        })

        $pageNumber.bind('pageNumber:change', function() {
            $talbesearch.submit();
        })
        $order.bind('order:change', function() {
            $talbesearch.submit();
        })
        var $sortable = $('th.th-sortable');
        if ($order.val().length > 0) {
            var sortable_item = $('[data-sort-name="' + $order.val() + '"]'),
                b = ($orderBy.val() == 'asc'); 
            sortable_item.adddClass('select', b)
            sortable_item.append(b ? '<span class="th-sort">' +
                '<i class="fa fa-sort-down text-active"></i>' +
                '<i class="fa fa-sort-up text"></i>' +
                '  <i class="fa fa-sort"></i>' +
                '  </span>' : '<span class="th-sort">' +
                '  <i class="fa fa-sort-down text"></i>' +
                '   <i class="fa fa-sort-up text-active"></i>' +
                '  <i class="fa fa-sort"></i>' +
                '  </span>') 

            sortable_item.siblings(".th-sortable").append('<span class="th-sort">' +
                '<i class="fa fa-sort-down text-active"></i>' +
                '<i class="fa fa-sort-up text-active"></i>' +
                '  <i class="fa fa-sort"></i>' +
                '  </span>')
        }else{
            $sortable.append('<span class="th-sort">' +
                '<i class="fa fa-sort-down text-active"></i>' +
                '<i class="fa fa-sort-up text-active"></i>' +
                '  <i class="fa fa-sort"></i>' +
                '  </span>');
        };
        
        $sortable.on('click', function() {
            var $this = $(this),
                name = $this.data('sortName'),
                val=$order.val();
                $order.val(name);
            var b = val==name?$this.hasClass('select'):true;
            $orderBy.val(b ? 'desc' : 'asc');
            $talbesearch.submit();

        })
        $("button[type='submit']", $talbesearch).bind('click', function() {
            var $required = $('[data-rule-required="true"]');
            if ($required.length > 0) {
                if ($.trim($required.val()).length == 0) {
                    config.msg.info("请输入搜索关键词")
                    return false;
               }
            }


        });

    };


});
